package com.siruier.boss.ui.activity.user

import android.content.Intent
import android.graphics.Typeface
import android.view.View
import com.siruier.boss.R
import com.siruier.boss.databinding.ActivityLetterCommitmentBinding
import com.siruier.boss.ui.activity.commom.SignatureActivity
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.ext.toColor
import com.siruier.boss.ui.ext.toastMessageLong
import com.siruier.boss.ui.ext.value
import com.siruier.boss.ui.helper.AppConfigHelper.loadConfig
import com.siruier.boss.ui.utils.GlideUtils
import java.io.File

class LetterCommitmentActivity : BaseActivity<ActivityLetterCommitmentBinding>() {
    private var signatureFile: File? = null
    override fun initView() {
        loadConfig {
            vb.webView.loadUrl(it.appMobileAgreement)
        }

        vb.titleBar.rightStr = "确定"
        vb.titleBar.rightTextView.apply {
            setTextColor(R.color.main_color.toColor())
            typeface = Typeface.DEFAULT_BOLD
        }
        bindViewClick(vb.titleBar.rightTextView, vb.buttonSign)
        signatureFile = intent?.extras?.getSerializable("signatureFile") as? File
        GlideUtils.intoView(this, signatureFile, vb.ivSign)

        intent?.extras?.getString("phone")?.let {
            vb.etPhone.setText(it)
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.buttonSign -> launchStartActivityForResult(SignatureActivity::class.java) {
                signatureFile = it.data?.getSerializableExtra("signatureFile") as File
                GlideUtils.intoView(this, signatureFile, vb.ivSign)
            }
            vb.titleBar.rightTextView -> {
                if (signatureFile == null) {
                    "请先签名".toastMessageLong()
                    return
                }
                if (vb.etPhone.length() != 11) {
                    "手机号输入错误".toastMessageLong()
                    return
                }
                setResult(RESULT_OK, Intent().apply {
                    putExtra("signatureFile", signatureFile)
                    putExtra("phone", vb.etPhone.value)
                })
                finish()
            }
        }
    }
}